package com.webtest.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.webtest.config.ExcludeEmptyQueryWrapper;
import com.webtest.dao.BookShelfMapper;
import com.webtest.dao.admin.AdminShelfMapper;
import com.webtest.entity.Book;
import com.webtest.entity.Shelf;
import com.webtest.service.admin.AdminShelfService;
import com.webtest.vo.SearchBooks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminShelfController {
    @Autowired
    private AdminShelfService adminShelfService;
    @Autowired
    private AdminShelfMapper adminShelfMapper;

    @Autowired
    private BookShelfMapper bookShelfMapper;

    //列表
    @RequestMapping("/shelf")
    public String showShelfs(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 6);
        List<Shelf> all = adminShelfService.list(new ExcludeEmptyQueryWrapper<>());


        // 为书架做数据统计，当前可借阅和比例
        SearchBooks searchBooks = new SearchBooks();
        for (Shelf shelf : all) {
            searchBooks.setShelfId(shelf.getShelfId());
            List<Book> bookByShelfs = bookShelfMapper.getAllBookByShelfId(searchBooks);
            int countSum = bookByShelfs.stream().mapToInt(Book::getCount).sum();
            shelf.setNum(String.valueOf(countSum));
            shelf.setShelfNum(countSum);

            // 计算比例
            double ratio = (double) (countSum) / shelf.getShelfMaxNum();
            shelf.setShelfRate(ratio);
        }


        PageInfo<Shelf> pageInfo = new PageInfo<>(all);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/shelf";
    }

    //条件搜索
    @PostMapping("/shelf/search")
    public String searchShelfs(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               Model model, Shelf searchShelfs) {
        PageHelper.startPage(pageNum, 6);
        List<Shelf> all;
        String ShelfName = searchShelfs.getShelfName();
        if (ShelfName == null) {
            all = adminShelfService.list(new ExcludeEmptyQueryWrapper<>());
        } else {
            all = adminShelfService.list(new ExcludeEmptyQueryWrapper<Shelf>().like("shelf_Name", searchShelfs.getShelfName()));
        }

        // 为书架做数据统计，当前可借阅和比例
        SearchBooks searchBooks = new SearchBooks();
        for (Shelf shelf : all) {
            searchBooks.setShelfId(shelf.getShelfId());
            List<Book> bookByShelfs = bookShelfMapper.getAllBookByShelfId(searchBooks);
            int countSum = bookByShelfs.stream().mapToInt(Book::getCount).sum();
            shelf.setNum(String.valueOf(countSum));
            shelf.setShelfNum(countSum);

            // 计算比例
            double ratio = (double) (countSum) / shelf.getShelfMaxNum();
            shelf.setShelfRate(ratio);
        }

        PageInfo<Shelf> list = new PageInfo<Shelf>(all);
        model.addAttribute("pageInfo", list);
        return "admin/shelf::shelfList";
    }

    @GetMapping("/shelf/input")
    public String toAddShelfs(Model model) {
        model.addAttribute("shelf", new Shelf());
        return "admin/shelf-input";
    }

    @PostMapping("/shelf/add")
    public String addShelfs(Shelf shelf, RedirectAttributes attributes) {

        adminShelfService.save(shelf);
        attributes.addFlashAttribute("message", "添加成功！！！");
        return "redirect:/admin/shelf";
    }

    //通过图书id获取图书信息，传给前端回显
    @GetMapping("/shelf/{id}/update")
    public String toUpdateShelfs(@PathVariable Long id, Model model) {
        Shelf shelf = adminShelfService.getById(id);
        model.addAttribute("shelf", shelf);
        return "admin/shelf-input";
    }

    @PostMapping("/shelf/update")
    public String updateShelfs(Shelf shelf, RedirectAttributes attributes) {
        if (shelf != null && adminShelfService != null) {
            shelf.setShelfRate(((double) shelf.getShelfNum() / (double) shelf.getShelfMaxNum()));
            adminShelfService.updateById(shelf);
            attributes.addFlashAttribute("message", "修改成功！！！");
        } else {
            // 处理空引用情况，例如打印日志或者返回错误消息
            // logger.error("Shelf or adminShelfService is null");
            // attributes.addFlashAttribute("error", "操作失败，请重试！");
        }
        return "redirect:/admin/shelf";
    }
    // public String updateShelfs(Shelf shelf,RedirectAttributes attributes){
    //    shelf.setShelfRate(((double) shelf.getShelfNum() / (double)shelf.getShelfMaxNum()));
    //    adminShelfService.updateById(shelf);

    //     attributes.addFlashAttribute("message", "修改成功！！！");
    //   return "redirect:/admin/shelf";
    // }

    @GetMapping("/shelf/{id}/delete")
    public String deleteShelfs(@PathVariable Long id, RedirectAttributes attributes) {
        adminShelfService.removeById(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/shelf";
    }
}
